package com.ljl.org.service.impl;

import com.ljl.basic.exception.BusinessException;
import com.ljl.basic.service.impl.BaseServiceImpl;
import com.ljl.basic.util.BaiduAuditUtils;
import com.ljl.basic.util.MD5Utils;
import com.ljl.basic.util.StrUtils;
import com.ljl.org.domain.Employee;
import com.ljl.org.domain.Shop;
import com.ljl.org.domain.ShopAuditLog;
import com.ljl.org.dto.ShopDto;
import com.ljl.org.mapper.EmployeeMapper;
import com.ljl.org.mapper.ShopAuditLogMapper;
import com.ljl.org.mapper.ShopMapper;
import com.ljl.org.service.IShopService;
import com.ljl.user.domain.Logininfo;
import com.ljl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper  shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public void settlement(Shop shop) {
        //一：校验
        //1.空值校验 - 试一下
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress()) || StringUtils.isEmpty(shop.getEmployee().getUsername())
                || StringUtils.isEmpty(shop.getEmployee().getEmail()) || StringUtils.isEmpty(shop.getEmployee().getPhone())
                || StringUtils.isEmpty(shop.getEmployee().getPassword())){
            throw new BusinessException("数据不能为空");
        }
        //2.密码是否一致：不做，如果要做在t_employee加一个字段comfirmPassword即可

        //3.校验店铺的名称是否合规
        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if(!boo){
            throw new BusinessException("店铺名称不合规!!!");
        }

        //4.是否曾经入驻过
        Shop shopTmp = shopMapper.loadByMessage(shop);
        if(shopTmp != null){
            throw new BusinessException("已经入驻，请直接登录。忘记密码请联系管理员!!!");
        }

        //二：添加到数据库
        //1.添加t_employee
        Employee employee = shop.getEmployee();
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        String md5 = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(md5);

        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());

        employeeMapper.save(employee);

        //2.添加t_shop
        shop.setAdmin_id(employee.getId());
        shopMapper.save(shop);

        //3.修改t_employee的shop_id
        employee.setShop_id(shop.getId());
        employeeMapper.update(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Transactional
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        // 更新状态
        Shop shop = shopMapper.findById(log.getShop_id());
        shop.setState(2);
        shopMapper.update(shop);

        // 审核日志
        log.setState(2);
        log.setAudit_id(325L);
        shopAuditLogMapper.update(log);

        // 发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1214171913@qq.com");
        helper.setSubject("店铺激活邮件");
        Long id = shop.getId();
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<img src='http://dfs.java.itsource.cn/"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+id+"'>点击该链接激活</a>",true);
        //收件人
        Employee employee = employeeMapper.findById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("447712920@qq.com");
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

    @Transactional
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        // 更新状态
        Shop shop = shopMapper.findById(log.getShop_id());
        shop.setState(4);
        shopMapper.update(shop);

        // 审核日志
        log.setState(2);
        log.setAudit_id(325L);
        shopAuditLogMapper.update(log);

        // 发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1214171913@qq.com");
        helper.setSubject("店铺入驻审核失败邮件");
        helper.setText("<h1>你的店铺入驻审核失败!!!</h1>" +
                "<a href='http://localhost:8081/#/register'>点击该链接重新入驻</a>",true);
        //收件人
        Employee employee = employeeMapper.findById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("447712920@qq.com");
        javaMailSender.send(mimeMessage);


    }


}
